package fr.mywiki.view.web.struts.form.structure;

import java.rmi.RemoteException;
import java.util.Collection;
import java.util.Locale;

import javax.ejb.FinderException;
import javax.servlet.http.HttpServletRequest;

import fr.mywiki.business.structure.StructureLite;
import fr.mywiki.business.structure.TreeManager;
import fr.mywiki.model.ejbAdapter.DataController;

public class SelectSingleStructController extends SelectStructureController {
	/** The type of this controller : single */
	public static final String CONTROLLER_TYPE = "single";
	
	/** Id of the structure */
	private Long idStructure;

	public Long getIdStructure() {
		return idStructure;
	}
	
	/** Current structure element */
	private StructureLite structure;
	
	public StructureLite getStructure() {
		return structure;
	}

	public void setStructure(StructureLite structure) {
		this.structure = structure;
	}

	public void setIdStructure(Long idStructure) throws RemoteException {
		this.idStructure = idStructure;
		if (idStructure == null)
			setStructure(null);
		else
			setStructure((StructureLite) DataController.getValue(idStructure,
					StructureLite.TYPE_ENT));
	}
	
	/**
	 * Affects the current element of the tree and centralize the tree on it.
	 * 
	 * @param structure
	 *          a <code>StructureLite</code> object.
	 */
	public void centerTree(StructureLite structure) throws RemoteException,
			FinderException {
		if (structure == null)
			setIdStructure(null);
		else
			setIdStructure(structure.getId());

		setExpandJS(TreeManager.getExpandCodeJS(structure));
	}
	
	/**
	 * Creates a new <code>SelectSingleStructComponent</code> object from the
	 * request.
	 */
	public static SelectSingleStructController createController(
			HttpServletRequest request) throws RemoteException, FinderException {
		SelectSingleStructController controller = new SelectSingleStructController();

		controller.initTree(request);

		return controller;
	}
	
	/**
	 * Creates a new <code>SelectStructureComponent</code> object from the
	 * request, intialized with the given structure.
	 * 
	 * @param request
	 *          the request
	 * @param structure
	 *          the <code>StructureLite</code> object to init the tree with.
	 * @return a <code>SelectStructureController</code> object, centralized on
	 *         structure.
	 */
	public static SelectSingleStructController createController(
			HttpServletRequest request, StructureLite structure) throws RemoteException, FinderException {
		SelectSingleStructController controller = createController(request);
		controller.centerTree(structure);
		return controller;
	}
	
	private SelectSingleStructController() {
		this(null, null, null, null,null);
	}

	public SelectSingleStructController(String tree, String rootName) {
		this(tree, rootName, null, null,null);
	}

	public SelectSingleStructController(String tree, String rootName,
			String expandJS, String rootImage, StructureLite structure) {
		setRootName(rootName);
		setTree(tree);
		setExpandJS(expandJS);
		setRootImage(rootImage);
		setStructure(structure);
	}

	public String getControllerType() {
		return CONTROLLER_TYPE;
	}
	
	public String loadTree(Locale locale) throws RemoteException, FinderException {
		Collection nodes = TreeManager.getTreeRootNodes();

		return TreeManager.buildTree(nodes, locale, TreeManager.TYPE_SINGLE);
	}
}
